package cn.ubest.mvvmcore.utils;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.io.File;

import cn.ubest.mvvmcore.R;


/**
 * Created by maoqi on 2020/4/12.
 */
public class GlideUtils {

    public static void displayHeader(Context context, String url, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.default_avatar)
//                .error(R.drawable.default_avatar)
                .into(imageView);
    }

    public static void displayHeader(Context context, File file, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(file)
//                .placeholder(R.drawable.default_avatar)
//                .error(R.drawable.default_avatar)
                .into(imageView);
    }

    public static void display(Context context, String url, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .into(imageView);
    }

    public static void display(Context context, String url, Target target) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .into(target);
    }

    public static void display(Context context, File file, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(file)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .into(imageView);
    }

    public static void display(Context context, @DrawableRes int drawableId, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        Glide.with(context)
                .load(drawableId)
                .into(imageView);
    }

    public static void displayRound(Context context, String url, ImageView imageView) {
        displayRound(context, url, imageView, ResourcesUtils.getDimens(R.dimen.dp_4));
    }

    public static void displayRoundVideoCover(Context context, String url, ImageView imageView) {
        displayRoundVideoCover(context, url, imageView, ResourcesUtils.getDimens(R.dimen.dp_4), new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                return false;
            }
        });
    }

    public static void displayRoundVideoCover(Context context, String url, ImageView imageView, RequestListener listener) {
        displayRoundVideoCover(context, url, imageView, ResourcesUtils.getDimens(R.dimen.dp_4), listener);
    }

    public static void displayRound(Context context, @DrawableRes int drawableId, ImageView imageView) {
        displayRound(context, drawableId, imageView, ResourcesUtils.getDimens(R.dimen.dp_4));
    }

    public static void displayRound(Context context, Uri uri, ImageView imageView) {
        displayRound(context, uri, imageView, ResourcesUtils.getDimens(R.dimen.dp_4));
    }

    public static void displayRoundNoPlaceholder(Context context, String url, ImageView imageView, int round) {
        if (isActivityDestroy(context)) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public static void displayRound(Context context, String url, ImageView imageView, int round) {
        if (isActivityDestroy(context)) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .apply(options)
                .into(imageView);
    }

    public static void displayRoundVideoCover(Context context,
                                              String url,
                                              ImageView imageView,
                                              int round,
                                              RequestListener listener) {
        if (isActivityDestroy(context)) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_cover_error_place_holder)
                .listener(listener)
                .apply(options)
                .into(imageView);
    }

    public static void displayRound(Context context, @DrawableRes int drawableId, ImageView imageView, int round) {
        if (isActivityDestroy(context)) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(drawableId)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .apply(options)
                .into(imageView);
    }

    public static void displayCircleHeader(Context context, String url, ImageView imageView) {
        if (isActivityDestroy(context)) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .transform(new CircleCrop());
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.default_avatar)
//                .error(R.drawable.default_avatar)
                .apply(options)
                .into(imageView);
    }

    public static void displayRound(Context context, Uri uri, ImageView imageView, int round) {
        if (isActivityDestroy(context)) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(round);
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), roundedCorners);
        Glide.with(context)
                .load(uri)
//                .placeholder(R.drawable.bg_place_holder)
//                .error(R.drawable.bg_place_holder)
                .apply(options)
                .into(imageView);
    }

    public static boolean isActivityDestroy(Context context) {
        if (context == null) {
            return true;
        }
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (activity.isFinishing() || activity.isDestroyed()) {
                return true;
            }
        }
        return false;
    }

}
